
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using SqlSugar.DistributedSystem.Snowflake;
using SqlSugarRepository;
using System.CodeDom;
using System.Data;
using winform分层框架;
using DbType = SqlSugar.DbType;
using Microsoft.Extensions.Configuration;
using AntdUI;
using BusinessService.Plugin.Helper;
using WindowsForm;
using BusinessService.Plugin.DataSource.Sqlite;
using BusinessService.Plugin.Const;
using BusinessService.Plugin.Services;
using BusinessService.Plugin.Services.Dtos;

namespace EMSSevice
{
    internal static class Program
    {

        public static IServiceProvider IOC { get; set; }
        /// <summary>
        ///  主程序入口
        /// </summary>
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.
            ApplicationConfiguration.Initialize();
            //创建服务容器
            var services = new ServiceCollection();
            //配置服务
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(@"Settings\dbsettings.json");
            IConfigurationRoot config = builder.Build();
            //添加服务注册
            ConfigureServices(services, config);
            //先用DI容器生成 serviceProvider, 然后通过 serviceProvider 获取Main Form的注册实例
            var serviceProvider = services.BuildServiceProvider();
            var formMain = serviceProvider.GetRequiredService<MainForm>();   //主动从容器中获取FormMain实例, 这是简洁写法	
                                                                             //
            IOC = serviceProvider;
            try
            {
                //启动窗体
                Application.Run(formMain);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "信息提示");
                return;
            }

        }
        /// <summary>
        /// 注入服务
        /// </summary>
        /// <param name="services"></param>
        public static void ConfigureServices(IServiceCollection services, IConfigurationRoot config)
        {
            //批量注入
            services.AddScoped<BackService>();//IBackService,
            services.AddSingleton<TransferDtos>();
            services.AddSingleton<JsonFile>();
            services.AddScoped(typeof(SqlSugarRepository<>));////注入仓储
            services.AddScoped<StaticResource>();////注入静态资源
            services.AddScoped(typeof(MainForm));


            //注册SqlSugar
            #region 注册SqlSugar 
            services.AddSingleton<ISqlSugarClient>(s =>
            {
                var sqlSugar = new SqlSugarScope(new List<ConnectionConfig>()
                {
                    new ConnectionConfig()
                {        ConfigId="0" ,
                           DbType = Settings1.Default.DBselect.Equals("MySql".Trim())? SqlSugar.DbType.MySql: SqlSugar.DbType.QuestDB,
                         ConnectionString =Settings1.Default.ConnectionStr,// config["ConnectionStrings:DefaultDbString"],
                         IsAutoCloseConnection = true,
                },
                new ConnectionConfig()
                {        ConfigId="1" ,
                         DbType = DbType.Sqlite,
                         ConnectionString ="datasource=localDB.db; ",
                         IsAutoCloseConnection = true,
                }

                },
                 db =>
                 {

                     db.Aop.OnLogExecuting = (sql, pars) =>
                     {
                         //aop代码

                         Log.Info(sql);
                     };
                 });
                return sqlSugar;
            });
            #endregion
            services.AddScoped<SqliteLibrary>();//注入Sqlite库方法



        }
    }

}